<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="115" />
    <FormPanel ref="formPanel1" :list="formList1" :formType="type" @formChange="formChangeHandle1" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <FormPanel ref="formPanel2" :list="formList2" :formType="type" @formChange="formChangeHandle2" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDrawer">返 回</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryRetailOrderById } from '@/api/dealerSale/SalesSanagement/RetailOrderQueryAF';
import { getEmployees, queryVinForOrder, queryPotentialCustomer } from '@/api/dealerSale/SalesSanagement/RetailOrderReview';
import { getLoginUser } from '@/api/cms-api/projectOperating/program/departmentInquire'; //获取登录信息

export default {
  mixins: [dictionary],

  props: {
    type: {
      type: String,
      default: 'show'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  // components: {
  //   SearchCstmr,
  //   SearchVin
  // },
  data() {
    this.saveData = null;
    this.saveData1 = null;
    this.saveData2 = null;
    return {
      formList: this.createdFrom(),
      formList1: this.createdFrom1(),
      formList2: this.createdFrom2(),
      isShowCstmr: false,
      isShowVin: false,
      formData: {},
      editData: {},
      detailData: null,
      cstmrData: null,
      vinData: null
    };
  },
  mounted() {
    this.init();
    this.queryEmployees();
    this.getDealerName();
  },
  methods: {
    init() {
      if (this.type != 'add') {
        this.getInfoById();
      }
    },
    // 获取登录信息
    async getDealerName() {
      const res = await getLoginUser();
      if (res.resultCode == 200) {
        this.formList.find(x => x.fieldName == 'dealerName').initialValue = res.data.dealerName;
      }
    },
    // 查询销售顾问
    async queryEmployees() {
      const res = await getEmployees();
      if (res.resultCode == 200) {
        res.data.map(x => {
          this.formList.find(item => item.fieldName == 'salesManId').itemList.push({ text: x.userName, value: x.userId });
        });
      }
    },
    // 根据ID查详细
    async getInfoById() {
      const res = await queryRetailOrderById(this.rowData.orderId);
      if (res.resultCode == 200) {
        this.detailData = res.data;
        this.formList.map(x => {
          // 潜客信息替换页面信息vsCustomerDTO 客户编号、客户姓名、手机号、证件类型、证件号、省市区、联系地址
          if (
            x.fieldName == 'customerNo' ||
            x.fieldName == 'customerName' ||
            x.fieldName == 'customerType' ||
            x.fieldName == 'customerPhone' ||
            x.fieldName == 'cardType' ||
            x.fieldName == 'cardNo' ||
            x.fieldName == 'provinceId' ||
            x.fieldName == 'cityId' ||
            x.fieldName == 'districtId' ||
            x.fieldName == 'linkAddress'
          ) {
            let data = res.data.vsCustomerDTO;
            this.formList.find(x => x.fieldName == 'cityId').itemList = this.createcityList(data.provinceId);
            this.formList.find(x => x.fieldName == 'districtId').itemList = this.district(data.cityId);
            if (x.fieldName == 'customerPhone') {
              x.initialValue = data['mobile'];
            } else {
              x.initialValue = data[x.fieldName];
            }
          } else {
            x.initialValue = res.data[x.fieldName];
          }
        });
        this.formList1.map(x => {
          let orderVinShowDto = res.data.orderVinShowDto || res.data;

          if (x.fieldName == 'salesQuatation') {
            x.initialValue = res.data['salesQuatation'];
          } else if (x.fieldName == 'discountAmount') {
            let result = 0;
            let basicPrice = orderVinShowDto.basicPrice;
            if (!basicPrice) return;
            result = parseFloat(basicPrice) - parseFloat(res.data.salesQuatation);
            x.initialValue = result || 0;
          } else if (x.fieldName == 'checkDesc') {
            x.initialValue = res.data['checkDesc'];
          } else {
            x.initialValue = orderVinShowDto[x.fieldName];
          }
        });

        this.formList2.map(x => {
          x.initialValue = res.data[x.fieldName];
        });
      } else {
        this.$notify.error({ title: '提示信息', message: res.errMsg });
      }
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    formChangeHandle1(val) {
      this.saveData1 = val;
    },
    formChangeHandle2(val) {
      this.saveData2 = val;
    },
    closeDrawer() {
      this.$emit('close', false, '');
    },
    // 查询潜客
    cstmrNoHandle() {
      this.isShowCstmr = true;
    },
    closeDialog(val, data) {
      this.isShowCstmr = val;
      this.cstmrData = data[0];
      this.setFormData(data[0]);
      console.log(data, '---关闭潜客---');
    },
    closeDialog1(val, data) {
      this.isShowVin = val;
      this.vinData = data[0];
      this.setFormData(data[0]);
      console.log(data, '---关闭VIN---');
    },
    setFormData(data) {
      for (let key in data) {
        this.formList.map(x => {
          if (x.fieldName == key) {
            x.initialValue = data[x.fieldName];
          }
        });
      }
    },
    // 查询VIN
    vinHandle() {
      this.isShowVin = true;
    },
    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '零售信息'
        },
        {
          type: 'INPUT',
          label: '零售单号',
          fieldName: 'orderNo',
          placeholder: '自动生成',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '客户编号',
          fieldName: 'customerNo',
          placeholder: '',
          readonly: true,
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={() => this.cstmrNoHandle()}></el-button>;
          }
        },
        {
          type: 'SELECT',
          label: '客户类型',
          fieldName: 'customerType',
          placeholder: '请选择',
          itemList: this.createDictList('1523')
        },
        {
          type: 'INPUT',
          label: '客户姓名',
          fieldName: 'customerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '手机号',
          fieldName: 'customerPhone',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '企业名称',
          fieldName: 'enterpriseName',
          placeholder: ''
        },
        {
          type: 'SELECT',
          label: '证件类型',
          fieldName: 'cardType',
          placeholder: '请选择',
          itemList: this.createDictList('1508')
        },
        {
          type: 'INPUT',
          label: '证件号',
          fieldName: 'cardNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '纳税人识别号',
          fieldName: 'taxpayerNumer',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          placeholder: '请选择',
          itemList: this.createRegionsList(),
          change: p => {
            if (p) {
              this.formList.map(x => {
                if (x.fieldName == 'cityId') {
                  x.initialValue = '';
                  x.itemList = this.createcityList(p);
                }
              });
            } else {
              this.formList.map(x => {
                if (x.fieldName == 'cityId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
                if (x.fieldName == 'districtId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
            }
          }
        },
        {
          type: 'SELECT',
          label: '城市',
          fieldName: 'cityId',
          placeholder: '请选择',
          itemList: [],
          change: p => {
            if (p) {
              this.formList.map(x => {
                if (x.fieldName == 'districtId') {
                  x.initialValue = '';
                  x.itemList = this.district(p);
                }
              });
            } else {
              this.formList.map(x => {
                if (x.fieldName == 'districtId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
            }
          }
        },
        {
          type: 'SELECT',
          label: '县区',
          fieldName: 'districtId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '联系地址',
          fieldName: 'linkAddress',
          placeholder: '请输入',
          selfCols: 2
        },
        {
          type: 'INPUT',
          label: '经销商',
          fieldName: 'dealerName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '购车用途',
          fieldName: 'buyPurpose',
          placeholder: '请选择',
          itemList: this.createDictList('1511')
        },
        {
          type: 'INPUT',
          label: '开单人',
          fieldName: 'sheetManName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'DATE',
          label: '签约日期',
          fieldName: 'signDate',
          dateType: 'exactdate',
          placeholder: '请选择'
        },
        {
          type: 'DATE',
          label: '预交车时间',
          fieldName: 'expectDate',
          dateType: 'exactdate',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '销售顾问',
          fieldName: 'salesManId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('6026')
        },
        {
          type: 'SELECT',
          label: '支付方式',
          fieldName: 'payType',
          placeholder: '请选择',
          itemList: this.createDictList('6015')
        },
        {
          type: 'INPUT',
          label: '首付款',
          fieldName: 'downPayment',
          maxlength: 9,
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '贷款金额',
          fieldName: 'loanAmount',
          maxlength: 9,
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '推荐人姓名',
          fieldName: 'referencesName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '推荐人电话',
          fieldName: 'referencesPhone',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '核销码',
          fieldName: 'verificationCode',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '支付意向金',
          fieldName: 'isIntentionMoney',
          itemList: this.createDictList('1004')
        },
        {
          type: 'INPUT',
          label: '意向金金额',
          fieldName: 'intentionMoney',
          maxlength: 9,
          offsetRightCols: 1,
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '支付大定',
          fieldName: 'isLargeFixedAmount',
          itemList: this.createDictList('1004')
        },
        {
          type: 'INPUT',
          label: '大定金额',
          fieldName: 'largeFixedAmount',
          offsetRightCols: 1,
          maxlength: 9,
          placeholder: '请输入'
        },
        // {
        //   type: 'SELECT',
        //   label: '大客户订单',
        //   fieldName: 'isMajor',
        //   itemList: this.createDictList('1004')
        // },
        // {
        //   type: 'SELECT',
        //   label: '特殊订单',
        //   fieldName: 'isSpecial',
        //   offsetRightCols: 1,
        //   itemList: this.createDictList('1004')
        // },
        {
          type: 'SELECT',
          label: '极狐APP客户订单',
          fieldName: 'isJihuApp',
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '是否在犹豫期内',
          fieldName: 'isHesitationPeriod',
          offsetRightCols: 1,
          itemList: this.createDictList('1004')
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          selfCols: 3,
          placeholder: ''
        }
      ];
    },
    createdFrom1() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '车辆信息'
        },
        {
          type: 'INPUT',
          label: '产品代码',
          fieldName: 'productCode',
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '品牌名称',
          fieldName: 'brandName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '车系名称',
          fieldName: 'seriesName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '车型名称',
          fieldName: 'modelName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配置名称',
          fieldName: 'packageName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '选装名称',
          fieldName: 'optionName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '内饰',
          fieldName: 'trimColorName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '外色',
          fieldName: 'colorName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '',
          readonly: true,
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={() => this.vinHandle()}></el-button>;
          }
        },
        {
          type: 'INPUT',
          label: '发动机号',
          fieldName: 'engineNo',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '电机号',
          fieldName: 'motorCode',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '电池号',
          fieldName: 'batteryNo',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '合格证号',
          fieldName: 'certificateNo',
          placeholder: '',
          offsetRightCols: 1,
          disabled: true
        },

        {
          type: 'INPUT',
          label: '销售指导价',
          fieldName: 'basicPrice',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '销售报价',
          fieldName: 'salesQuatation',
          placeholder: '请输入',
          maxlength: 9,
          onInput: val => {
            if (val) {
              let result = 0;
              let basicPrice = this.formList1.find(x => x.fieldName === 'basicPrice').initialValue;
              if (!basicPrice) return;
              result = parseFloat(basicPrice) - parseFloat(val);
              this.formList1.find(x => x.fieldName === 'discountAmount').initialValue = result || 0;
              return;
            }
            this.formList1.find(x => x.fieldName === 'discountAmount').initialValue = '';
          }
        },
        {
          type: 'INPUT',
          label: '折让金额',
          fieldName: 'discountAmount',
          placeholder: '',
          disabled: true
        },
        {
          type: 'TEXT_AREA',
          label: '审批意见',
          fieldName: 'checkDesc',
          selfCols: 3,
          placeholder: '',
          disabled: true
        }
      ];
    },
    createdFrom2() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '指标人信息'
        },
        {
          type: 'SELECT',
          label: '指标人类型',
          fieldName: 'indicatorType',
          offsetRightCols: 2,
          itemList: this.createDictList('1523'),
          change: val => {
            this.setItems(val);
          }
        },
        {
          type: 'INPUT',
          label: '车主姓名',
          fieldName: 'indicatorName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '证件类型',
          fieldName: 'indicatorCardType',
          placeholder: '请选择',
          itemList: this.createDictList('1508')
        },
        {
          type: 'INPUT',
          label: '证件号',
          fieldName: 'indicatorCardNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '企业名称',
          fieldName: 'indicatorEnterpriseName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '征信代码',
          fieldName: 'creditCode',
          placeholder: '',
          disabled: true,
          offsetRightCols: 1
        },
        {
          type: 'INPUT',
          label: '家庭/企业地址',
          fieldName: 'address',
          placeholder: '请输入',
          selfCols: 2
        },
        {
          type: 'INPUT',
          label: '邮政编码',
          fieldName: 'zipCode',
          placeholder: ''
        }
      ];
    }
  }
};
</script>
